package com.xss.idea_plugin_box;

import com.intellij.notification.*;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.util.Arrays;
import java.util.stream.Collectors;

import static com.intellij.configurationStore.StorageUtilKt.NOTIFICATION_GROUP_ID;

/**
 * @author 马晓涛
 * @since 2025/7/16
 */
public class FormatSQLAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        if(editor == null){
            return;
        }
        Document document = editor.getDocument();
        VirtualFile file = FileDocumentManager.getInstance().getFile(document);
        if(file == null){
            return;
        }
        String fileName = file.getNameWithoutExtension();
        if(!fileName.endsWith("Mapper")){
            return;
        }
        SelectionModel selectionModel = editor.getSelectionModel();
        String selectedText = selectionModel.getSelectedText();
        if (selectedText == null || selectedText.isEmpty()) {
            return;
        }

        if(!selectedText.contains("@Select")){
            return;
        }

        Project project = e.getProject();
        NotificationGroup notificationGroup = NotificationGroupManager.getInstance().getNotificationGroup("pluginsBox.Notification");

        String formatSql = Arrays.stream(selectedText.split("\\+")).map(m->{
            int start = m.indexOf("\"");
            start = start == -1? 0:start;
            int end = m.lastIndexOf("\"");
            end = end == -1? m.length():end + start;
            String substring = m.substring(start + 1, end - start);
            return substring.replaceAll("([\\\\][tfn])+", " ")+"\n";
        }).collect(Collectors.joining( ));

        Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection stringSelection = new StringSelection(formatSql);
        systemClipboard.setContents(stringSelection, null);

        Notification notification = notificationGroup.createNotification(
                "成功提取可执行SQL", // 标题
                formatSql, // 内容
                NotificationType.INFORMATION // 类型：INFO/WARN/ERROR
        );
        // 5. 显示通知（可指定项目上下文）
        notification.notify(project);
    }
}
